<?php
function router_current_module($dblayer) {
$modref = null;
	$modref = GetParam("module","text","GET");
	if ($modref) {
		$result = $dblayer->query("SYSTEM",sprintf("select * from modules where modref='%s' and enabled = 1 LIMIT 1",$modref));
		if (!$result->num_rows) {
			$modref = null;
		}
	}
	if (!$modref) {
		if ($result = $dblayer->query("SYSTEM",sprintf("select data from variables where label = 'system_default_module' order by varid ASC LIMIT 1"))) {
			$row = $result->fetch_object();
			$modref = $row->data;
		}
	}
return $modref;
}
function router_load_module($Q,$dblayer,$modref) {
global $default;
	if ($modref) {
		//$sysdb = $default->param("SYSTEM","dbname");
		//$sysconn = $default->param("SYSTEM","dbconn");
		if (module_exists($dblayer,$modref)) {
			//$permitted_module_members = permitted_module_members($default,$modref);
			//if (count($permitted_module_members)) {
				$user = current_user($dblayer);
				$user_groups = get_user_groups($dblayer,$user['id'],$modref);
				if (count($user_groups)) {
					//we are members of groups that are associated with this module ... permitted
					//$module = new mysqldb;
					//$module->setquery($sysdb,$sysconn,sprintf("select * from modules where modref = '%s' and enabled = 1",$modref));
					$module = $dblayer->query("SYSTEM",sprintf("select * from modules where modref = '%s' and enabled = 1",$modref));
						if ($module->num_rows) {
							$mrow = $module->fetch_object();
							$source = $mrow->location;
							$ref = "";
							switch ($source) {
							case "system":
								$ref = "system";
							break;
							case "site":
								$ref = "site";
							break;
							}//switch location
							$mfile = $Q{'absolute_path'}."/{$ref}/modules/{$modref}/{$modref}.php";
								if (file_exists($mfile)) {
									require_once($mfile);
									$dbfile = $Q{'absolute_path'}."/{$ref}/modules/{$modref}/db.php";
									if (file_exists($dbfile)) {
										$default->loadDBconfigFile($dbfile,$dblayer);
									}
									return true;
								} else {
									//echo "file {$mfile} does not exist.";
								}
						} else {
							//echo "!module->totalRows";
						}
					//$module->disposequery();
				} else {
					//echo "!count(user_groups)";
				}
			//}
		} else {
			//echo "Module does not exist.";
		}
	}
	return false;
}
function module_exists($dblayer,$modref) {
$exists = false;
	if ($modref) {
		$module = $dblayer->query("SYSTEM",sprintf("select * from modules where modref='%s'",$modref));
			if ($module->num_rows) {
				$exists = true;
			}
	}
return $exists;
}
function permitted_module_members($dblayer,$modref) {
$members = array();
	if (module_exists($dblayer,$modref)) {
		$module = $dblayer->query("SYSTEM",sprintf("select mid from modules where modref='%s' and enabled = 1",$modref));
			if ($module->num_rows) {
				$mrow = $module->fetch_object();
				$mid = $mrow->mid;
				$groups = $dblayer->query("USER",sprintf("select gid from module_members where mid = %d",$mid));
					if ($groups->num_rows) {
						while ($grow = $groups->fetch_object()) {
							$members[] = $grow->gid;
						}//while
					}
			}
	}
return $members;
}
function permitted_modules($dblayer,$id) {
$modules = array();
	$m = $dblayer->query("SYSTEM",sprintf("select * from modules where enabled = 1"));
		if ($m->num_rows) {
			while ($row = $m->fetch_object()) {
				$groups = get_user_groups($dblayer,$id,$row->modref);
				if (count($groups)) {
					$modules[] = $row->mid;
				}
			}//while
		}
return $modules;
}
function module_id($dblayer,$modref) {
$id = null;
	if ($modref) {
		$module = $dblayer->query("SYSTEM",sprintf("select id from modules where modref='%s'",$modref));
			if ($module->num_rows) {
				$row = $module->fetch_object();
				$id = $row->id;
			}
	}
return $id;
}
?>
